package studio.xiaoyun.web;

import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 * 封装http响应中的异常信息
 * @author 岳正灵
 * @since 1.0.0
 */
@XmlRootElement(name = "error")
public class HttpError {
	/**
	 * 异常信息
	 */
	private String message;
	/**
	 * 异常堆栈
	 */
	private List<String> stack;
	
	@XmlElement
	public String getMessage() {
		return message;
	}
	
	@XmlJavaTypeAdapter(ListXMLAdapter.class)
	@XmlElement
	public List<String> getStack() {
		return stack;
	}
	
	public void setMessage(String message) {
		this.message = message;
	}
	
	public void setStack(List<String> stack) {
		this.stack = stack;
	}
	
	public void setStack(Exception exception) {
		stack = new LinkedList<String>();
		stack.add(exception.getClass().getName());
		StackTraceElement[] elements = exception.getStackTrace();
		for(int i=0;i<elements.length;i++){
			String line = elements[i].getClassName()+"#"+elements[i].getMethodName()+"(line:"+elements[i].getLineNumber()+")";
			stack.add(line);
			if(i>18){
				break;
			}
		}
	}

}
